package xyz.wbsite.zentao.sdk.user;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.log.StaticLog;
import xyz.wbsite.zentao.sdk.ZentaoInstance;
import xyz.wbsite.zentao.sdk.user.ent.ZtUser;
import xyz.wbsite.zentao.sdk.user.req.ZtUserListAllRequest;
import xyz.wbsite.zentao.sdk.user.rsp.ZtUserListAllResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 禅道用户服务
 *
 * @author wangbing
 * @version 0.0.1
 * @since 1.8
 */
public class ZtUserService {

    private ZentaoInstance zentaoInstance;

    public ZtUserService(ZentaoInstance client) {
        this.zentaoInstance = client;
    }

    public ZtUserListAllResponse listAll(ZtUserListAllRequest request) {
        ZtUserListAllResponse response = new ZtUserListAllResponse();
        try {
            List<ZtUser> result = new ArrayList<>();
            int currentPage = 1;
            while (true) {
                String url = StrUtil.format("/company-browse-inside-0-bydept-id-32-2000-{}.html", currentPage);
                StaticLog.info(url);
                HttpRequest httpRequest = zentaoInstance.createGetForHtml(url);
                HttpResponse execute = httpRequest.execute();
                if (HttpStatus.HTTP_OK != execute.getStatus()) {
                    response.setSuccess(false);
                    response.setMessage("请求失败");
                    return response;
                }
                String body = execute.body();
                Document parse = Jsoup.parse(body);
                Elements trs = parse.select(".main-table #userList tbody tr");
                for (Element tr : trs) {
                    ZtUser ztUser = new ZtUser();
                    ztUser.setId(Convert.toInt(tr.select(".c-id").text()));
                    ztUser.setName(tr.child(1).attr("title"));
                    ztUser.setAccount(tr.child(2).text());
                    ztUser.setPosition(tr.child(3).text());
                    ztUser.setEmail(tr.child(4).text());
                    ztUser.setSex(tr.child(5).text());
                    ztUser.setPhone(tr.child(6).text());
                    ztUser.setQq(tr.child(7).text());
                    ztUser.setLastLoginDate(tr.child(8).text());
                    ztUser.setAccessTimes(Convert.toInt(tr.child(9).text(), 0));
                    result.add(ztUser);
                }
                if (trs.size() < 2000) {
                    break;
                }
                // 继续获取下一页数据
                currentPage++;
            }
            response.setResult(result);
            response.setSuccess(true);
            response.setMessage("请求成功");
            return response;
        } catch (HttpException e) {
            StaticLog.error(e);
        }
        response.setSuccess(false);
        response.setMessage("请求失败");
        return response;
    }
}
